package com.jcl.recruit.core.config;

import com.jcl.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName SystemConfig
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/12 19:33
 * @Version 1.0
 **/
@Component
public class SystemConfig {

    private static final Logger logger                                      = LoggerFactory.getLogger(SystemConfig.class);

    private static final String FILE_NAME                                   = "deployment";
    private static final String FILE_TYPE                                   = "properties";
    private static final String FILE_SPLIT                                  = "-";
    private static final String SPLIT                                       = ".";

    // basic
    private static final String SYSTEM_REGISTER_PASSWORD_DEFAULT            = "system.register.password";
    private static final String SYSTEM_LOGO_URL                             = "system.logo.url";
    private static final String REGISTER_PASSWORD_DEFAULT                   = "123456";
    private static final String SYSTEM_WEB_URL                              = "system.weburl";
    private static final String SYSTEM_UPLOAD_FILE_SUFFIX                   = "system.upload.file.suffix";

    // wechat
    private static final String SYSTEM_WECHAT_APPID                         = "system.wechat.appid";
    private static final String SYSTEM_WECHAT_APPSECRET                     = "system.wechat.appsecret";
    private static final String WECHAT_TEMPLATE_SCREEN_ID                   = "wechat.template.screen.id";
    private static final String WECHAT_TEMPLATE_DOWNLOAD_RESUME_ID          = "wechat.template.download.resume.id";
    private static final String WECHAT_LOGIN_URL                            = "wechat.login.url";

    // ftp
    private static final String SYSTEM_FTP_SERVER                           = "storage.ftp.server";
    private static final String SYSTEM_FTP_PORT                             = "storage.ftp.port";
    private static final String SYSTEM_FTP_USERNAME                         = "storage.ftp.username";
    private static final String SYSTEM_FTP_PASSWORD                         = "storage.ftp.password";
    private static final String SYSTEM_FTP_LOCATION                         = "storage.ftp.location";

    // other
    private static final String SYSTEM_PUSH_CRAWLING_USER                   = "system.push.crawling.user";

    private static String current_version                                   = "prod";
    private static String version_dev                                       = "dev";

    private static Properties props ;

    public SystemConfig(){

    }

    public static void newInstance(String version){

        String filePath = FILE_NAME  + SPLIT + FILE_TYPE;
        if(null != version && !version_dev.equals(version)){
            SystemConfig.current_version = version;
            filePath = FILE_NAME + FILE_SPLIT + current_version + SPLIT + FILE_TYPE;
        }
        if(null == props){
            try {
                Resource resource = new ClassPathResource(filePath);
                props = PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    public static String getUploadFileSuffix(){
        return getProperty(SYSTEM_UPLOAD_FILE_SUFFIX);
    }

    public static String getSystemWechatAppid(){
        return getProperty(SYSTEM_WECHAT_APPID);
    }

    public static String getSystemWechatAppsecret(){
        return getProperty(SYSTEM_WECHAT_APPSECRET);
    }

    public static String getWechatTemplateScreenId(){
        return getProperty(WECHAT_TEMPLATE_SCREEN_ID);
    }

    public static String getWechatTemplateDownloadResumeId(){
        return getProperty(WECHAT_TEMPLATE_DOWNLOAD_RESUME_ID);
    }

    public static String getWechatLoginUrl(){
        return getProperty(WECHAT_LOGIN_URL);
    }

    public static String getSystemWebUrl(){
        return getProperty(SYSTEM_WEB_URL);
    }

    public static String getSystemFtpServer(){
        return getProperty(SYSTEM_FTP_SERVER);
    }

    public static String getSystemFtpPort(){
        return getProperty(SYSTEM_FTP_PORT);
    }

    public static String getSystemFtpUsername(){
        return getProperty(SYSTEM_FTP_USERNAME);
    }

    public static String getSystemFtpPassword(){
        return getProperty(SYSTEM_FTP_PASSWORD);
    }

    public static String getSystemFtpLocation(){
        return getProperty(SYSTEM_FTP_LOCATION);
    }



    public static String getSystemLogo(){

        return getSystemWebUrl() + getProperty(SYSTEM_LOGO_URL);
    }

    public static String getDefaultPassword(){
        String password = getProperty(SYSTEM_REGISTER_PASSWORD_DEFAULT);
        if(StringUtils.isEmpty(password)){
            password = REGISTER_PASSWORD_DEFAULT;
        }
        return password;
    }

    public static Map<String, String> getCrawlingPushUser(){
        Map<String, String> map = new HashMap<>();
        String crawling = getProperty(SYSTEM_PUSH_CRAWLING_USER);
        if(StringUtils.hasLength(crawling)){
            String[] arr1 = crawling.split(",");
            for(String arr: arr1){
                String[] temp = arr.split("\\|");
                map.put(temp[0], temp[1]);
            }
        }
        return map;
    }

    public static void setCurrent_version(String current_version) {
        SystemConfig.current_version = current_version;
    }

    /**
     * 获取属性
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return props == null ? null :  props.getProperty(key);

    }

    /**
     * 获取属性
     * @param key 属性key
     * @param defaultValue 属性value
     * @return
     */
    public static String getProperty(String key,String defaultValue){
        return props == null ? null : props.getProperty(key, defaultValue);
    }

    /**
     * 获取properyies属性
     * @return
     */
    public static Properties getProperties(){
        return props;
    }
}
